<?php

/**
 * 定时更新wiki关联的内容的admin_score 以及addtime
 *
 **/

use Doraemon\model\Es\MainContent;
use Doraemon\service\es\EsObjRelateWikiSupplement;
use Biz\service\wiki\ObjRelateWiki as sObjRelateWiki;
use Doraemon\tools\Log;

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

//先从obj_relate_wiki_supplement_ol 查询内容admin_score 或addtime =0的内容id  再去main_content_ol 更新数据

$data = EsObjRelateWikiSupplement::search([
    'admin_score' => 0,
    'addtime' => 0,
    'pagesize' => 9999,
    'page' => 1,
]);
$data = $data['rows'] ?? [];
if($data){
    $objIds = array_column($data, 'obj_id');
    $objInfoMap = MainContent::getBatchByIds(array_unique($objIds));
    echo '总共需要修复：'.count($objIds).'条数据'.PHP_EOL;
    $i = 0;
    $j = 0;
    foreach ($objInfoMap as $item){
        $addtime= $item['addtime'] ?? 0;
        $adminScore= $item['admin_score'] ?? 0;
        if ($addtime> 0) {
            $res = sObjRelateWiki::syncAddtimeToEs($item['id'], $addtime);
            if (!$res) {
                Log::error('syncAddtimeToEs-更新失败', ['obj_id' => $item['id'],'addtime'=>$addtime]);
            }
            $i++;
        }
        if ($adminScore> 0) {
            $res = sObjRelateWiki::syncAdminScoreToEs($item['id'], $adminScore);
            if (!$res) {
                Log::error('syncAdminScoreToEs-更新失败', ['obj_id' => $item['id'],'admin_score'=>$adminScore]);
            }
            $j++;
        }
    }
    echo '总共修改addtime：'.$i.'次'.PHP_EOL;
    echo '总共修改admin_score：'.$j.'次'.PHP_EOL;
}




